﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Lottery.Client.SL.Lang;
using Lottery.Client.SL.Mode;

namespace Lottery.Client.SL
{
    public class PublicFunction
    {
        //\\
        public static int CheckSingleNumber(string singleNumber, ref int playNumberLength, ref string sumCount,
                                            ref int sumNum)
        {
            int result = 0;
            if (!string.IsNullOrEmpty(singleNumber))
            {
                int num = 3;
                string text;
                if (DataCache.CurrentPlayInfo.LotteryID == 4 || DataCache.CurrentPlayInfo.LotteryID == 6 ||
                    DataCache.CurrentPlayInfo.LotteryID == 29)
                {
                    text = "4 6 29";
                }
                else
                {
                    text = "not 4 6 29";
                }
                if (text.Contains(DataCache.CurrentPlayInfo.PlayTypeID))
                {
                    num = 2;
                }
                sumNum = 0;
                string playTypeName = CulbViewMode.SelectGame.SelectGameMenu.SelectGameMode.PlayTypeName;
                var stringBuilder = new StringBuilder();
                string[] array = singleNumber.Split(new[]
                                                        {
                                                            ','
                                                        });
                for (int i = 0; i < array.Length; i++)
                {
                    string text2 = array[i];
                    if (!string.IsNullOrEmpty(text2) && text2.Trim() != "")
                    {
                        if (playTypeName.Contains("单式"))
                        {
                            stringBuilder.Append(text2.Trim());
                        }
                        else
                        {
                            stringBuilder.Append(SortString(text2.Trim()));
                        }
                    }
                }
                string[] array2 = stringBuilder.ToString().Trim().Split(new[]
                                                                            {
                                                                                ' '
                                                                            });
                if (playTypeName.Contains("单式"))
                {
                    for (int j = 0; j < array2.Length; j++)
                    {
                        if (!IsNumeric(array2[j].Trim()) || array2[j].Trim().Length != num)
                        {
                            result = j + 1;
                            sumNum = 0;
                            break;
                        }
                        sumNum += GetDsNum(num, array2[j].Trim());
                    }
                }
                else
                {
                    for (int k = 0; k < array2.Length; k++)
                    {
                        if (!IsNumeric(array2[k].Trim()) || array2[k].Trim().Length != num)
                        {
                            result = k + 1;
                            sumNum = 0;
                            break;
                        }
                        sumNum++;
                    }
                }
                if (sumNum > 0)
                {
                    sumCount = stringBuilder.ToString().Trim();
                }
                playNumberLength = num;
            }
            return result;
        }

        public static string SortString(string data)
        {
            if (string.IsNullOrWhiteSpace(data))
            {
                return data;
            }
            char[] array = data.ToCharArray();
            Array.Sort(array);
            return new string(array);
        }

        public static bool IsNumeric(string str)
        {
            var regex = new Regex(@"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
");
            return regex.IsMatch(str);
        }

        public static bool IsDouble(string str)
        {
            var regex = new Regex(@"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
");
            return regex.IsMatch(str);
        }

        public static double StrToDouble(string scount)
        {
            double result = 0.0;
            if (IsDouble(scount))
            {
                result = double.Parse(scount);
            }
            return result;
        }

        public static int StrToInt32(string scount)
        {
            int result = 0;
            if (IsNumeric(scount))
            {
                result = Convert.ToInt32(scount);
            }
            return result;
        }

        public static void InitLotteryInfo()
        {
            if (DataCache.LotteryDatas != null && DataCache.LotteryDatas.Count > 0)
            {
                /*
              *  --- 天津时时彩,重庆时时彩,新疆时时彩，江西时时彩 ---
                 * 三星直选,60.30#万位,千位,百位#前3直选复式*20#@#前3直选单式*35#千位,百位,十位#中3直选复式*22#@#中3直选单式*25#百位,十位,个位#后3直选复式*18#@#后3直选单式
                 * :
                 * 三星组选,61.31#+#前3组六复式*32#+#前3组三复式*21#@#前3组选单式*14#+#后3组六复式*15#+#后3组三复式*13#@#后3组选单式
                 * :
                 * 二星直选,62.29#万位,千位#前2直选复式*28#@#前2直选单式*09#十位,个位#后2直选复式*19#@#后2直选单式
                 * :
                 * 二星组选,63.34#+#前2组选复式*33#@#前2组选单式*08#+#后2组选复式*07#@#后2组选单式
                 * :
                 * 大小单双,64.42#万位,千位#前2大小单双*04#十位,个位#后2大小单双
                 * :
                 * 定位胆,65.41#万位,千位,百位,十位,个位#5星定位胆
                 * :
                 * 不定胆,66.17#+#后3不定胆*27#+#前3不定胆*38#+#中3不定胆*24#+#5星不定胆
              
              *$ --- 数字、福彩、上海时时乐 ---
                 
                 * 三星直选,60.02#百位,十位,个位#直选复式*01#@#直选单式
                 * :
                 * 三星组选,61.04#+#组六复式*05#+#组三复式*03#@#组选单式
                 * :
                 * 二星直选,62.09#百位,十位#前2直选复式*08#@#前2直选单式*11#十位,个位#后2直选复式*10#@#后2直选单式
                 * :
                 * 定位胆,64.06#百位,十位,个位#定位胆
                 * :
                 * 不定胆,63.07#+#不定位胆
*/
                //:游戏大类
                //,游戏名称
                //
                string text = lotteryConfig.lotteryInfo;
                var gameKind = new GameKind();
                gameKind.Games = new List<Game>();
                for (int i = 0; i < DataCache.LotteryDatas.Count; i++)
                {
                    var game = new Game();
                    game.GameID = DataCache.LotteryDatas[i].ID;
                    game.GameName = DataCache.LotteryDatas[i].Name;
                    game.OpenChase = DataCache.LotteryDatas[i].OpenChase;
                    if (DataCache.LotteryDatas[i].ID == 4 || DataCache.LotteryDatas[i].ID == 6 ||
                        DataCache.LotteryDatas[i].ID == 29)
                    {
                        game.KindType = 1;
                    }
                    else
                    {
                        game.KindType = 0;
                    }
                    game.GamesMenu = new List<GameMenu>();
                    string[] array = text.Split('$')[game.KindType].Split(':');
                    string[] array2 = array;
                    //三星
                    for (int j = 0; j < array2.Length; j++)
                    {
                        string text2 = array2[j];
                        var gameMenu = new GameMenu();
                        //三星直选
                        gameMenu.TypeName = text2.Split('.')[0].Split(',')[0];
                        //60 || 61 || 62...
                        gameMenu.TypeID = int.Parse(text2.Split('.')[0].Split(',')[1]);
                        gameMenu.GameModes = new List<GameMode>();
                        //30#万位,千位,百位#前3直选复式
                        string[] array3 = text2.Split('.')[1].Split('*');
                        //前2直选式............gamemode
                        for (int k = 0; k < array3.Length; k++)
                        {
                            string text3 = array3[k];
                            string[] array4 = text3.Split('#');
                            var gameMode = new GameMode();
                            //前3直选复式
                            gameMode.PlayTypeName = array4[2];
                            //30
                            gameMode.PlayTypeID = array4[0];
                            //万位,千位,百位 || @ || +

                            gameMode.PlayTypeKeyBoard = array4[1] == "+" ? "键位" : array4[1];
                            gameMenu.GameModes.Add(gameMode);
                        }
                        game.GamesMenu.Add(gameMenu);
                    }
                    gameKind.Games.Add(game);
                }
                CulbViewMode.GameKind = gameKind;
            }
        }

        public static int GetDsNum(int playTypeLength, string sNumber)
        {
            if (playTypeLength == 3)
            {
                if (sNumber.ToCharArray()[0].ToString() == sNumber.ToCharArray()[1].ToString() &&
                    sNumber.ToCharArray()[0].ToString() == sNumber.ToCharArray()[2].ToString())
                {
                    return 1;
                }
                if (sNumber.ToCharArray()[0].ToString() == sNumber.ToCharArray()[1].ToString() ||
                    sNumber.ToCharArray()[0].ToString() == sNumber.ToCharArray()[2].ToString() ||
                    sNumber.ToCharArray()[1].ToString() == sNumber.ToCharArray()[2].ToString())
                {
                    return 3;
                }
                return 6;
            }
            else
            {
                if (sNumber.ToCharArray()[0].ToString() == sNumber.ToCharArray()[1].ToString())
                {
                    return 1;
                }
                return 2;
            }
        }

        public static bool CheckAgent(string Pageper)
        {
            bool result = false;
            if (DataCache.CurrentUserInfo != null && DataCache.CurrentUserInfo.UserType != 0)
            {
                result = DataCache.CurrentUserInfo.AgentPermissions.Contains(Pageper);
            }
            return result;
        }
    }
}